package com.changgou.framwork.exception;

import com.changgou.util.Result;
import com.changgou.util.StatusCode;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Author tang.qingbo
 * @Date 2020/8/22 0:23
 * @Version 1.0
 */
@ControllerAdvice//全局异常处理,相当于在方法前加上 try...
public class BaseExceptionHandler {
    /**
     * 运行期处理
     */
    @ExceptionHandler(RuntimeException.class)
    @ResponseBody
    public Result error(RuntimeException e) {
        e.printStackTrace();
        return new Result(false, StatusCode.ERROR, e.getMessage());
    }

    /**
     * 异常处理
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result error(Exception e) {
        e.printStackTrace();
        return new Result(false, StatusCode.ERROR, "系统繁忙,请联系管理员操作!");
    }
}
